/*
 * The National Parks Database Project
 * Group: Tyler Simrell, Vladmir Dudnic, Michael Jagielo
 * FieldParser.java
 */

package gui;

/**
 * This class has two static methods that parse data into queries, one that only takes in a trail name and
 * the other that takes all relevant data.  
 * @author Tyler Simrell
 * @version 5/24/13
 */
public class FieldParser 
{	
	
	private static final String STARTER_QUERY = "SELECT *\n" +
                                                "FROM trail_inventory, maint_user\n";//insert table names here
	
	private static final String TRAIL_NAME = "trail_inventory.trail_name";
	private static final String PARK_NAME = "park_name";
	private static final String VISTA_POINT = "vista";
	private static final String SLOPE = "slope";
	private static final String LENGTH = "length";
	private static final String TERRAIN = "terrain";
	private static final String DIFFICULTY = "difficulty";
	
	/**
	 * This method parses the data into an SQL query used for querying a server for a specific
	 * piece of information
	 * @param trail_name Name of the trail
	 * @param park_name Name of the National Park
	 * @param vista_point A major vista on the trail
	 * @param slope The slope of the trail (difference of height from the starting point to the ending point
	 * @param length The length of the trail.
	 * @param terrain The terrain of the trail.
	 * @param difficulty The difficulty of the trail.
	 * @return This method returns the finished SQL query in string form
	 */
	public static String parseFieldsResults(String trailName, String parkName, String vistaPoint,
			   String slope, String length, String terrain, String difficulty)
	{
		StringBuilder query = new StringBuilder(STARTER_QUERY);
		query.append("WHERE trail_inventory.trail_name = maint_user.trail_name AND ");
		
		if(!trailName.isEmpty())
		{
			query.append(TRAIL_NAME + " = ");
			query.append("\"" + trailName + "\"");
			query.append(" AND ");
		}
		
		if(!parkName.isEmpty())
		{
			query.append(PARK_NAME + " = ");
			query.append("\"" + parkName + "\"");
			query.append(" AND ");
		}
		
		if(!vistaPoint.isEmpty())
		{
			query.append(VISTA_POINT + " = ");
			query.append("\"" + vistaPoint + "\"");
			query.append(" AND ");
		}
		
		if(!slope.isEmpty())
		{
			query.append(SLOPE + " = ");
			query.append(slope);
			query.append(" AND ");
		}
		
		if(!length.isEmpty())
		{
			query.append(LENGTH + " = ");
			query.append(length);
			query.append(" AND ");
		}
			
		if(!terrain.isEmpty())
		{
			query.append(TERRAIN + " = ");
			query.append("\"" + terrain + "\"");
			query.append(" AND ");
		}
		
		if(!difficulty.isEmpty())
		{
			query.append(DIFFICULTY + " = ");
			query.append("\"" + difficulty + "\"");
		}
		
		String queryString = query.toString();
		queryString = normalize(queryString);
		queryString = queryString + ";";
		
		return queryString;
	}
	
	/**
	 * This method chops off extraneous "AND" for giving to a SQL interpreter.  
	 * @param query The query to be fixed
	 * @return The fixed query
	 */
	private static String normalize(String query)
	{
		if(query.substring(0, 3).equals("AND"))
		{
			query = query.substring(3, query.length());
		}
		
		if(query.substring(query.length() - 4, query.length()).equals("AND "))
		{
			query = query.substring(0, query.length() - 5);
		}
		return query;
	}
}
